package com.jqgrid.search.Jqgrid;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SearchOperationUtil {


    private static Map<String, String> operator = new HashMap<String, String>();

    static {
        operator = new HashMap<String, String>();
        operator.put("eq", " = ");
        operator.put("ne", " <> ");
        operator.put("lt", " < ");
        operator.put("le", " <= ");
        operator.put("gt", " > ");
        operator.put("ge", " >= ");
        operator.put("bw", " LIKE ");
        operator.put("bn", " NOT LIKE ");
        operator.put("in", " IN ");
        operator.put("ni", " NOT IN ");
        operator.put("ew", " LIKE ");
        operator.put("en", " NOT LIKE ");
        operator.put("cn", " LIKE ");
        operator.put("nc", " NOT LIKE ");
    }

    public static void main(String[] args) {

        if (StringUtils.containsIgnoreCase("sssDate", "date")) {
            System.out.println("ww");

        }
    }

    private static String getStringForRule(Rules rules) {
        String opUF = operator.get(rules.getOp()), opC = "", ret, val, field;

        val = rules.getData();
        opC = rules.getOp();
        field = rules.getField();
        if (opC.equals("bw") || opC.equals("'bn'")) {
            val = val + "%";
        }
        if (opC.equals("ew") || opC.equals("'en'")) {
            val = "%" + val;
        }
        if (opC.equals("cn") || opC.equals("'nc'")) {
            val = "%" + val + "%";
        }
        if (opC.equals("in") || opC.equals("'ni'")) {
            val = " (" + val + ")";
        }

        if (StringUtils.containsIgnoreCase(field, "date") || StringUtils.containsIgnoreCase(field, "time")) {

            val = "'" + val + "'";

        }

        ret = underscoreName(field) + " " + opUF + " " + val;


        return ret;
    }


    public static String getStringForGroup(Groups groups) {
        int index;
        String s = "(";
        if (groups.getGroups() != null) {
            for (index = 0; index < groups.getGroups().size(); index++) {
                if (s.length() > 1) {
                    s += " " + groups.getGroupOp() + " ";
                }
                try {
                    s += getStringForGroup(groups.getGroups().get(index));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        if (groups.getRules() != null) {
            try {
                for (index = 0; index < groups.getRules().size(); index++) {
                    if (s.length() > 1) {
                        s += " " + groups.getGroupOp() + " ";
                    }
                    s += getStringForRule(groups.getRules().get(index));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        s += ")";

        if (s.equals("()")) {
            return ""; // ignore groups that don't have rules
        }
        return s;
    }

    /**
     * 转换为下划线
     *
     * @param camelCaseName
     * @return
     */
    private static String underscoreName(String camelCaseName) {
        StringBuilder result = new StringBuilder();
        if (camelCaseName != null && camelCaseName.length() > 0) {
            result.append(camelCaseName.substring(0, 1).toLowerCase());
            for (int i = 1; i < camelCaseName.length(); i++) {
                char ch = camelCaseName.charAt(i);
                if (Character.isUpperCase(ch)) {
                    result.append("_");
                    result.append(Character.toLowerCase(ch));
                } else {
                    result.append(ch);
                }
            }
        }
        return result.toString();
    }

}

